package com.candoit.webapps.pricing.controllers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.candoit.webapps.pricing.model.ArmadoManoDeObra;
import com.candoit.webapps.pricing.model.Articulo;
import com.candoit.webapps.pricing.model.MateriaPrima;
import com.candoit.webapps.pricing.model.MateriaPrimaArticulo;
import com.candoit.webapps.pricing.model.MateriaPrimaArticuloFK;
import com.candoit.webapps.pricing.model.TipoLinea;
import com.candoit.webapps.pricing.model.TipoMateriaPrima;
import com.candoit.webapps.pricing.util.ArticuloUtil;
import com.candoit.webapps.pricing.util.MateriaPrimaConsumo;
import com.candoit.webapps.pricing.util.QueryUtil;

@ManagedBean(name = "articuloCtrl")
@SessionScoped
public class ArticuloController extends AbstractController {
	
	private Articulo articuloSelected;
	
	private MateriaPrima materiaPrimaSelected;
	
	private double consumo;
	
	private MateriaPrimaArticulo mpArticulo;
	
	private List<MateriaPrimaConsumo> consumos;
	
	private List <ArticuloUtil> articulos;
	
	public List<ArticuloUtil> getArticulos(){
		List<Articulo> list = QueryUtil.getInstance().getArticulos();
		this.fillArticulos(list);
		return articulos;
	}
	
	public void setArticulos(List<ArticuloUtil> articulos) {
		this.articulos = articulos;
	}

	public Articulo getArticuloSelected() {
		if (articuloSelected == null){
			articuloSelected = new Articulo();
		}
		return articuloSelected;
	}

	public void setArticuloSelected(Articulo articuloSelected) {
		this.articuloSelected = articuloSelected;
	}
	
	public MateriaPrima getMateriaPrimaSelected() {
		return materiaPrimaSelected;
	}

	public void setMateriaPrimaSelected(MateriaPrima materiaPrimaSelected) {
		this.materiaPrimaSelected = materiaPrimaSelected;
	}
	
	public double getConsumo() {
		return consumo;
	}

	public void setConsumo(double consumo) {
		this.consumo = consumo;
	}

	public void clearData(){
		this.saveSuccess = false;
		this.deleteSuccess = false;
		this.articuloSelected = null;
		this.materiaPrimaSelected = null;
		this.consumo = 0.00d;
	}
	
	public void saveArticulo(){
		Iterator<ArticuloUtil> it = articulos.iterator();
		while (it.hasNext()){
			   ArticuloUtil util = (ArticuloUtil) it.next();
			   if (util.getArticulo().getId() == null || util.getArticulo().getId() == 0){
				   if (util.getArticulo().getDescripcion() != null && !util.getArticulo().getDescripcion().equals("")){
					   this.save(util.getArticulo());
				   }
			   }else{
				   this.update(util.getArticulo());
			   }
		}
	}
	
	public void removeArticulo(){
		Iterator<ArticuloUtil> it = articulos.iterator();
		while (it.hasNext()){
			ArticuloUtil util = (ArticuloUtil) it.next();
			if (util.isSelected() && util.getArticulo().getId() != null && util.getArticulo().getId() != 0){
				List<MateriaPrimaArticulo> matPArt = QueryUtil.getInstance().getMateriaPrimaArticulos(util.getArticulo());
				Iterator<MateriaPrimaArticulo> itMatPArt = matPArt.iterator();
				while (itMatPArt.hasNext()){
						MateriaPrimaArticulo matArt = (MateriaPrimaArticulo) itMatPArt.next();
						this.remove(matArt);
				}
				this.remove(util.getArticulo());
			}	
		}	
	}
	
	public void saveConsumoArticulo(){
       Iterator<MateriaPrimaConsumo> it = consumos.iterator();
       while (it.hasNext()){
    	   MateriaPrimaConsumo consumo = (MateriaPrimaConsumo) it.next();
    	   MateriaPrimaArticulo matArticulo = QueryUtil.getInstance().getMateriaPrimaArticulo(articuloSelected, consumo.getTipo());
    	   if (matArticulo == null){
    		   matArticulo = new MateriaPrimaArticulo();
    		   matArticulo.setConsummo(consumo.getConsumo());
    		   MateriaPrimaArticuloFK fk = new MateriaPrimaArticuloFK();
    		   fk.setArticulo(articuloSelected);
    		   fk.setMateriaPrima(consumo.getTipo());
    		   matArticulo.setMpArticuloFK(fk);
    		   this.save(matArticulo);
    	   }else{
    		   matArticulo.setConsummo(consumo.getConsumo());
    		   this.update(matArticulo);
    	   }
       }
       
       if( esArtesanal() && getArmado() != null ){
    	   this.articuloSelected.setArmado(getArmado());
    	   this.update(this.articuloSelected);
       }
	}

	public MateriaPrimaArticulo getMpArticulo() {
		return mpArticulo;
	}

	public void setMpArticulo(MateriaPrimaArticulo mpArticulo) {
		this.mpArticulo = mpArticulo;
	}

	public List<MateriaPrimaConsumo> getConsumos() {
		List<MateriaPrimaArticulo> matArticulos = QueryUtil.getInstance().getMateriaPrimaArticulos(articuloSelected);
		if (matArticulos.size() == 0 ){
			fillConsumos();
		}else{
			fillConsumosUpdate(matArticulos);
		}
		return consumos;
	}

	public void setConsumos(List<MateriaPrimaConsumo> consumos) {
		this.consumos = consumos;
	}
	
	private void fillConsumos(){
		consumos = new ArrayList<MateriaPrimaConsumo>();
		TipoLinea tipoLinea = articuloSelected.getFormato().getLinea().getTipoLinea();
		List<TipoMateriaPrima> tipos = QueryUtil.getInstance().getTipoMateriaPrimabyTipo(tipoLinea);
		Iterator<TipoMateriaPrima> it = tipos.iterator();
		while (it.hasNext()){
			TipoMateriaPrima tipo = (TipoMateriaPrima) it.next();
			MateriaPrimaConsumo consumo = new MateriaPrimaConsumo();
			consumo.setTipo(tipo);
			consumo.setConsumo(0.00d);
			consumos.add(consumo);
		}
	}
	
	private void fillConsumosUpdate(List<MateriaPrimaArticulo> lista){
		consumos = new ArrayList<MateriaPrimaConsumo>();
		Iterator<MateriaPrimaArticulo> it = lista.iterator();
		while (it.hasNext()){
			MateriaPrimaArticulo mat = (MateriaPrimaArticulo) it.next();
			MateriaPrimaConsumo consumo = new MateriaPrimaConsumo();
			consumo.setTipo(mat.getMpArticuloFK().getMateriaPrima());
			consumo.setConsumo(mat.getConsummo());
			consumos.add(consumo);
		}
	}
	
	public List<ArmadoManoDeObra> getArmados(){
		return QueryUtil.getInstance().getArmados();
	}
	
	public boolean esArtesanal(){
		return articuloSelected.getFormato().getLinea().getTipoLinea().getId() == TipoLinea.ARTESANAL;
	}
	
	private void fillArticulos(List<Articulo> list){
		articulos = new ArrayList<ArticuloUtil>();
		Iterator<Articulo> it = list.iterator();
		while (it.hasNext()){
			   Articulo articulo = (Articulo) it.next();
			   ArticuloUtil util = new ArticuloUtil();
			   util.setArticulo(articulo);
			   util.setSelected(false);
			   articulos.add(util);
		}
		
		ArticuloUtil util = new ArticuloUtil();
		Articulo articulo= new Articulo();
		util.setArticulo(articulo);
		util.setSelected(false);
		articulos.add(util);
	}

	public ArmadoManoDeObra getArmado() {
		return articuloSelected.getArmado();
	}

	public void setArmado(ArmadoManoDeObra armado) {
		this.articuloSelected.setArmado(armado);
	}
	
	
}
